package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
<#if datasource=="dsB">
import com.tmptSource.mapperbiz.${classNameFirstWordUpper}Mapper;
<#else>
import com.tmptSource.mapper.${classNameFirstWordUpper}Mapper;
</#if>
import com.tmptSource.bean.${classNameFirstWordUpper};
import com.tmptSource.dto.${classNameFirstWordUpper}Dto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.service.intf.I${classNameFirstWordUpper}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.apache.log4j.Logger;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="${classNameFirstWordLower}Service")
@Transactional(propagation = Propagation.REQUIRED)
public class ${classNameFirstWordUpper}Service implements I${classNameFirstWordUpper}Service {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired private DataLimitService dataLimitService;
    @Autowired private Mapper mapper;
    @Autowired private MyMapper myMapper;
    @Autowired private MyPageUtils myPageUtils;
    @Autowired
    private ${classNameFirstWordUpper}Mapper ${classNameFirstWordLower}Mapper;


    @Override
    public Result<${classNameFirstWordUpper}Dto> pagination(${classNameFirstWordUpper}Dto ${classNameFirstWordLower}Dto,PageDto pageDto) {
        <#if (sumTitleMap?keys?size>0) >
        //页面显示，列表footer区显示，如：合计 {"balancedate":"合计："}
        Map<String,String> sumTitleMap = new HashMap<>();
        <#list sumTitleMap?keys as key>
            sumTitleMap.put("${key}","${sumTitleMap[key]}");
        </#list>
        </#if>
        //分页查询
        Result<${classNameFirstWordUpper}Dto> result = myPageUtils.pagination(
                ${classNameFirstWordLower}Dto,pageDto,
                ${classNameFirstWordUpper}.class,
                ${classNameFirstWordUpper}Dto.class,
                ${classNameFirstWordLower}Mapper,
                <#if needSum==true>true<#else>false</#if>,<#if (sumTitleMap?keys?size>0) >sumTitleMap<#else>null</#if>);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param ${classNameFirstWordLower}Dto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<${classNameFirstWordUpper}Dto> pagination(${classNameFirstWordUpper}Dto ${classNameFirstWordLower}Dto,PageDto pageDto,Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        <#if (sumTitleMap?keys?size>0) >
        //页面显示，列表footer区显示，如：合计 {"balancedate":"合计："}
        Map<String,String> sumTitleMap = new HashMap<>();
        <#list sumTitleMap?keys as key>
        sumTitleMap.put("${key}","${sumTitleMap[key]}");
        </#list>
        </#if>
        //分页查询
        Result<${classNameFirstWordUpper}Dto> result = myPageUtils.pagination(
                ${classNameFirstWordLower}Dto,pageDto,
                ${classNameFirstWordUpper}.class,
                ${classNameFirstWordUpper}Dto.class,
                ${classNameFirstWordLower}Mapper,
                paramMap1,
                <#if needSum==true>true<#else>false</#if>,<#if (sumTitleMap?keys?size>0) >sumTitleMap<#else>null</#if>);

        return result;
    }


    @Override
    public Result<${classNameFirstWordUpper}Dto> selectAll() {
        Result<${classNameFirstWordUpper}Dto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<${classNameFirstWordUpper}> list = ${classNameFirstWordLower}Mapper.selectAll(mapParam);
        List<${classNameFirstWordUpper}Dto> dtoList = myMapper.map(list, ${classNameFirstWordUpper}Dto.class);
        result.setResultList(dtoList);

        return result.success();
    }

    @Override
    public Result<${classNameFirstWordUpper}Dto> selectById(long id) {
        Result<${classNameFirstWordUpper}Dto> result = new Result();

        ${classNameFirstWordUpper} ${classNameFirstWordLower} = ${classNameFirstWordLower}Mapper.selectById(id);
        ${classNameFirstWordUpper}Dto dto = mapper.map(${classNameFirstWordLower},${classNameFirstWordUpper}Dto.class);
        result.setResultObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<${classNameFirstWordUpper}Dto> addData(${classNameFirstWordUpper}Dto ${classNameFirstWordLower}Dto){
        Result<${classNameFirstWordUpper}Dto> result = new Result();

        //封闭数据
        ${classNameFirstWordUpper} ${classNameFirstWordLower} = mapper.map(${classNameFirstWordLower}Dto, ${classNameFirstWordUpper}.class);
        ${classNameFirstWordLower}.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = ${classNameFirstWordLower}Mapper.insert(${classNameFirstWordLower});
        if(i==0){
            return result.fault("插入失败");
        }

        ${classNameFirstWordUpper}Dto dto = mapper.map(${classNameFirstWordLower},${classNameFirstWordUpper}Dto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<${classNameFirstWordUpper}Dto> editData(${classNameFirstWordUpper}Dto ${classNameFirstWordLower}Dto){
        Result<${classNameFirstWordUpper}Dto> result = new Result();

        //封装数据
        ${classNameFirstWordUpper} ${classNameFirstWordLower} = mapper.map(${classNameFirstWordLower}Dto, ${classNameFirstWordUpper}.class);
        ${classNameFirstWordLower}.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = ${classNameFirstWordLower}Mapper.updateById(${classNameFirstWordLower});
        if(i==0){
            return result.fault("编辑数据失败");
        }

        ${classNameFirstWordUpper}Dto dto = mapper.map(${classNameFirstWordLower},${classNameFirstWordUpper}Dto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id,String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("${primaryKey}",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate",DateTool.getCurTimestamp());

        //调用mapper
        long i = ${classNameFirstWordLower}Mapper.deleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }




    <#list columnList as column>
    <#if column.dataResource?exists && column.dataResource==2 && column.optionsSql?exists && column.optionsSql!='' >
    /**
    *页面上查询列的值，动态加载，sql语句读数据库配置而来
    */
    public java.util.List select4${column.propertyName}(){
        return ${classNameFirstWordLower}Mapper.select4${column.propertyName}();
    }
    </#if>
    </#list>

}
